﻿using System;
using System.Web.Mvc;
using ML.Utils;

namespace ML.WebApi.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// api接口页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return Redirect("index.html");
        }

        /// <summary>
        /// 微信测试授权页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Wechat()
        {
            //var package = string.Format("prepay_id={0}", result.prepay_id);

            //ViewData["product"] = product;
            string strAppId = "wx09f4345fc8eaf58f";
            string strTimestamp = GetTimestamp();
            string strNoncestr = GetNoncestr();
            ViewData["appId"] = strAppId;
            ViewData["timeStamp"] = strTimestamp;
            ViewData["nonceStr"] = strNoncestr;
            //ViewData["package"] = package;
            //ViewData["paySign"] = GetJsPaySign(strAppId, strTimestamp, strNoncestr, package, TenPayV3Info.Key);
            return View();
        }

        public ActionResult Demo()
        {
            return View();
        }

        /// <summary>
        /// 随机生成Noncestr
        /// </summary>
        /// <returns></returns>
        private string GetNoncestr()
        {
            return CommonUtil.Md5(Guid.NewGuid().ToString()); 
        }

        /// <summary>
        /// 获取微信时间格式
        /// </summary>
        /// <returns></returns>
        private string GetTimestamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

        /// <summary>
        /// 获取UI使用的JS支付签名
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="timeStamp"></param>
        /// <param name="nonceStr"></param>
        /// <param name="package">格式：prepay_id={0}</param>
        /// <param name="signType"></param>
        /// <returns></returns>
        private string GetJsPaySign(string appId, string timeStamp, string nonceStr, string package, string key,
            string signType = "MD5")
        {
            //设置支付参数
            //paySignReqHandler.SetParameter("appId", appId);
            //paySignReqHandler.SetParameter("timeStamp", timeStamp);
            //paySignReqHandler.SetParameter("nonceStr", nonceStr);
            //paySignReqHandler.SetParameter("package", package);
            //paySignReqHandler.SetParameter("signType", signType);
            var paySign = CreateMd5Sign("key", key);
            return paySign;
        }

        /// <summary>
        /// 创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名
        /// </summary>
        /// <param name="key">参数名</param>
        /// <param name="value">参数值</param>
        /// key和value通常用于填充最后一组参数
        /// <returns></returns>
        public virtual string CreateMd5Sign(string key, string value)
        {
            //StringBuilder sb = new StringBuilder();

            //ArrayList akeys = new ArrayList(Parameters.Keys);
            //akeys.Sort(ASCIISort.Create());

            //foreach (string k in akeys)
            //{
            //    string v = (string)Parameters[k];
            //    if (null != v && "".CompareTo(v) != 0
            //        && "sign".CompareTo(k) != 0
            //        //&& "sign_type".CompareTo(k) != 0
            //        && "key".CompareTo(k) != 0)
            //    {
            //        sb.Append(k + "=" + v + "&");
            //    }
            //}

            //sb.Append(key + "=" + value);

            ////string sign = MD5UtilHelper.GetMD5(sb.ToString(), GetCharset()).ToUpper();

            ////编码强制使用UTF8：https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_1
            //string sign = MD5UtilHelper.GetMD5(sb.ToString(), "UTF-8").ToUpper();

            return "";
        }
    }
}